$(function () {
    //抽取变量
    var client_datagrid = $("#client_datagrid");
    var client_form = $("#client_form");
    var client_dialog = $("#client_dialog");
    var clientQuery_dialog = $("#clientQuery_dialog");
    var clientQuery_form = $("#clientQuery_form");
    var clientTraceAdd_form = $("#clientTraceAdd_form");
    var clientTraceAdd_dialog = $("#clientTraceAdd_dialog");
    var examRegit_dialog = $("#examRegit_dialog");
    var examRegit_form = $("#examRegit_form");
    var enroll_dialog = $("#enroll_dialog");
    var enroll_form = $("#enroll_form");
    //初始化datagrid
    client_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/client/list.do",
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        toolbar: "#client_toolbar",
        columns: [[
            {field: 'name', title: '姓名', width: 70, align: 'center'},
            {
                field: 'saleman', title: '营销人员', width: 70, align: 'center', formatter: function (value, row, index) {
                    return value ? value.username : "";
                }
            },
            {field: 'traceNum', title: '跟踪次数', width: 60, align: 'center'},
            {field: 'lastTraceTime', title: '最后跟踪时间', width: 140, align: 'center'},
            {field: 'appointTime', title: '约访日期', width: 100, align: 'center'},
            {field: 'nextTraceTime', title: '下次跟进时间', width: 140, align: 'center'},
            {field: 'qq', title: 'QQ', width: 80, align: 'center'},
            {field: 'tel', title: '电话', width: 80, align: 'center'},
            {field: 'school', title: '学校', width: 70, align: 'center'},
            {
                field: 'intentionDegree',
                title: '意向程度',
                width: 70,
                align: 'center',
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'campus', title: '意向校区', width: 70, align: 'center', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'clz', title: '意向班级', width: 70, align: 'center', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'state', title: '状态', width: 70, align: 'center', formatter: function (value, row, index) {
                    if (value && value.name == '正式学员') {
                        return "<b><span style='color:green;'>正式学员</span></b>"
                    } else {
                        return value ? value.name : "";
                    }
                }
            },
            {
                field: 'isTraced', title: '是否跟踪', width: 60, align: 'center', formatter: function (value, row, index) {
                    if (row.traceNum) {
                        return '已跟踪';
                    } else {
                        return "<span style='color: red'>未跟踪</span>";
                    }
                }
            },
            {field: 'remark', title: '备注', width: 60, align: 'center'}
        ]]
    });
    //初始化dialog
    client_dialog.dialog({
        width: 700,
        height: 500,
        buttons: "#client_btns",
        closed: true
    });
    // 高级查询对话框
    clientQuery_dialog.dialog({
        width: 800,
        height: 300,
        buttons: "#clientQuery_btns",
        closed: true
    });
    // 跟踪对话框
    clientTraceAdd_dialog.dialog({
        width: 800,
        height: 500,
        buttons: "#clientTraceAdd_btns",
        closed: true
    });
    //考试登记弹出框
    examRegit_dialog.dialog({
        width: 500,
        height: 300,
        title: "学员考试登记",
        buttons: "#examRegit_btns",
        closed: true
    });
    //转正
    enroll_dialog.dialog({
        width: 800,
        height: 500,
        title: "学员转正登记",
        buttons: "#enroll_btns",
        closed: true
    });
    //所有ab标签的方法都交给methodObject来管理
    var methodObjdect = {
        //新增潜在学员
        add: function () {
            $("#client_btns").show();
            $(".easyui-combobox").combo("readonly", false);
            client_form.form("clear");
            $("#client_form input").prop("disabled", false);
            client_dialog.dialog("setTitle", "新增潜在客户");
            client_dialog.dialog("open");
        },
        edit: function () {
            $("#client_btns").show();
            $(".easyui-combobox").combo("readonly", false);
            client_form.form("clear");
            //获取选中行的数据
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
            } else {
                if (row.eduBackground) {
                    row["eduBackground.id"] = row.eduBackground.id;
                }
                if (row.source) {
                    row["source.id"] = row.source.id;
                }
                if (row.introducerStu) {
                    row["introducerStu.id"] = row.introducerStu.id;
                }
                if (row.clientType) {
                    row["clientType.id"] = row.clientType.id;
                }
                if (row.intentionDegree) {
                    row["intentionDegree.id"] = row.intentionDegree.id;
                }
                if (row.state) {
                    row["state.id"] = row.state.id;
                }
                if (row.clz) {
                    row["clz.id"] = row.clz.id;
                }
                if (row.bigCustomer) {
                    row["bigCustomer.id"] = row.bigCustomer.id;
                }
                if (row.campus) {
                    row["campus.id"] = row.campus.id;
                }
                if (row.intentionSubject) {
                    row["intentionSubject.id"] = row.intentionSubject.id;
                }
                //将查询的数据加载进编辑表单中
                client_form.form("load", row);
                client_dialog.dialog("setTitle", "编辑潜在客户");
                client_dialog.dialog("open");
            }
        },
        //查看
        view: function () {
            client_form.form("clear");
            //获取选中行的数据
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
            } else {
                if (row.eduBackground) {
                    row["eduBackground.id"] = row.eduBackground.id;
                }
                if (row.source) {
                    row["source.id"] = row.source.id;
                }
                if (row.introducerStu) {
                    row["introducerStu.id"] = row.introducerStu.id;
                }
                if (row.clientType) {
                    row["clientType.id"] = row.clientType.id;
                }
                if (row.intentionDegree) {
                    row["intentionDegree.id"] = row.intentionDegree.id;
                }
                if (row.state) {
                    row["state.id"] = row.state.id;
                }
                if (row.clz) {
                    row["clz.id"] = row.clz.id;
                }
                if (row.bigCustomer) {
                    row["bigCustomer.id"] = row.bigCustomer.id;
                }
                if (row.campus) {
                    row["campus.id"] = row.campus.id;
                }
                if (row.intentionSubject) {
                    row["intentionSubject.id"] = row.intentionSubject.id;
                }
                //将查询的数据加载进编辑表单中
                $("#client_btns").hide();
                client_form.form("load", row);
                client_dialog.dialog("setTitle", "查看潜在客户");
                $(".easyui-combobox").combo("readonly", true);
                $("#client_form input").prop("disabled", true);
                client_dialog.dialog("open");
            }
        },
        //新增/编辑保存操作
        save: function () {
            client_form.form("submit", {
                url: "/client/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, 'info', function () {
                            client_dialog.dialog("close");
                            client_datagrid.datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, 'info', function () {
                            client_dialog.dialog("close");
                        })
                    }
                }
            })
        },
        //高级查询
        query: function () {
            $(".easyui-combobox").combo("readonly", false);
            clientQuery_form.form("clear");
            clientQuery_dialog.dialog("setTitle", "高级查询");
            clientQuery_dialog.dialog("open");
        },
        //提交查高级查询表单
        submitForm: function () {
            clientQuery_form.form("submit", {
                url: "/client/list.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    clientQuery_dialog.dialog("close");
                    //加载本地数据，旧的行将被移除
                    client_datagrid.datagrid("loadData", data);
                }
            })
        },
        //取消
        cancel: function () {
            client_dialog.dialog("close");
        },
        //高级查询弹框取消
        cancelQuery: function () {
            clientQuery_dialog.dialog("close");
        },
        //学员跟踪
        clientTrace: function () {
            $("[ data-sign]").prop("disabled", true);
            $("[ data-sign]").combo("readonly", true);
            var client = client_datagrid.datagrid("getSelected");
            console.log(client);
            if (!client) {
                $.messager.alert("温馨提示", "请选中一条数据!", "info");
                return;
            }
            clientTraceAdd_form.form("clear");
            clientTraceAdd_dialog.dialog("setTitle", "客户跟踪记录");
            //$("#clientTraceAdd_dialog").dialog("vcenter");
            clientTraceAdd_dialog.dialog("open");

            client["client.id"] = client.id;
            if (client.saleman) {
                client["saleman.id"] = client.saleman.id;
            }
            client["client.traceNum"] = client.traceNum;
            console.log(client.traceNum);
            if (client.bigCustomer) {
                client["university.id"] = client.bigCustomer.id;
                $(":checkbox").prop('checked', 'checked');
            }
            if (client.intentionDegree) {
                client["intentionDegree.id"] = client.intentionDegree.id;
            }
            if (client.state) {
                client["state.id"] = client.state.id;
            }
            if (client.clz) {
                client["clz.id"] = client.clz.id;
            }
            if (client.appointTime) {
                $("#client.appointTime").val(client.appointTime);
            }
            if (client.lastTraceTime) {
                $("#client.lastTraceTime").val(client.lastTraceTime);
            }
            if (client.nextTraceTime) {
                $("#client.nextTraceTime").val(client.nextTraceTime);
            }

            $("#clientTraceAdd_form").form("load", client);
        },
        //保存跟踪操作
        saveTrace: function () {
            clientTraceAdd_form.form("submit", {
                url: '/clientTrace/traced.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            if ($("#client_datagrid")) {
                                $("#client_datagrid").datagrid("reload");
                            } else if ($("#clientTrace_datagrid")) {
                                $("#clientTrace_datagrid").datagrid("reload");
                            }
                            clientTraceAdd_dialog.dialog("close");
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info");
                        clientTraceAdd_dialog.dialog("close");
                    }
                }
            })
        },
        //取消跟踪操作
        cancelTrace: function () {
            clientTraceAdd_dialog.dialog("close");
        },
        //放入资源池
        abandon: function () {
            var client = client_datagrid.datagrid("getSelected");
            if (!client) {
                $.messager.alert("温馨提示", "请选中一条数据!", "info");
                return;
            }
            if (client.state && client.state.name == '正式学员') {
                $.messager.alert("温馨提示", "正式学员不能放入资源池!", "info");
                return;
            }
            $.messager.confirm('温馨提示', "确定要将该客户放入资源池?", function (r) {
                if (r) {
                    $.get('/client/abandon.do', {clientId: client.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                client_datagrid.datagrid("load");
                            })
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    });
                }
            })
        },
        //刷新
        reload: function () {
            client_datagrid.datagrid("load");
        },
        //考试登记
        examRegist: function () {
            examRegit_form.form("clear");
            var client = client_datagrid.datagrid("getSelected");
            if (!client) {
                $.messager.alert("温馨提示", "请选择一行数据", 'info');
            }
            client["clz.id"] = client.clz.id;
            client["client.id"] = client.id;
            examRegit_form.form("load", client);
            examRegit_dialog.dialog("open");
        },
        //考试登记提交
        regist: function () {
            examRegit_form.form("submit", {
                url: '/test/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, 'info', function () {
                            examRegit_dialog.dialog("close");
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, 'info');
                    }
                }

            })
        },
        //取消登记
        abandonRegist: function () {
            examRegit_dialog.dialog("close");
        },
        //转正登记
        enroll: function () {
            enroll_form.form("clear");
            var client = client_datagrid.datagrid("getSelected");
            console.log(client);
            if (!client) {
                $.messager.alert("温馨提示", "请选择一行数据", 'info');
            }
            if (client.state && client.state.name == '正式学员') {
                $.messager.alert("温馨提示", "正式学员不能执行转正操作");
                return;
            }
            //转正操作
            client["clientName.id"] = client.id;
            client["clientName.name"] = client.name;

            if (client.eduBackground) {
                client["eduBackground.id"] = client.eduBackground.id;
            }
            if (client.source) {
                client["source.id"] = client.source.id;
            }
            if (client.clientType) {
                client["clientType.id"] = client.clientType.id;
            }
            if (client.clz) {
                client["classname.id"] = client.clz.id;
            }
            if (client.foreignLangLevel) {
                client["foreignlevel.id"] = client.foreignLangLevel.id;
            }
            if (client.saleman) {
                client["saleman.id"] = client.saleman.id;
            }
            if (client.campus) {
                client["campus.id"] = client.campus.id;
            }
            enroll_form.form("load", client);
            enroll_dialog.dialog("open");
        },
        //转正表单提交
        enrollSubmit: function () {
            var clientId = $("#clientId").val();
            console.log(clientId);
            enroll_form.form("submit", {
                url: '/studentofficial/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.get('/client/changeState.do', {clientId: clientId}, function (info) {
                            if (info.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    enroll_dialog.dialog("close");
                                    client_datagrid.datagrid("reload");
                                })
                            }
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, 'info');
                        enroll_dialog.dialog("close");
                    }
                }
            })
        },
        //取消提交转正表单
        enrollcancel: function () {
            enroll_dialog.dialog("close");
        },
        //取消编辑表单
        cancel: function () {
            client_dialog.dialog("close");
        },
        //过滤非正式学员操作
        filter: function () {
            $.get("/client/list.do?filter=true", function (data) {
                client_datagrid.datagrid("loadData", data)
            }, "json")
        },
        //xls文件导出
        exportXls: function () {
            window.location.href = "/client/exportXls.do";
        }
    };
//所有a标签对应的事件有method来调用
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        //调用具体的方法
        methodObjdect[cmd]();
    });
})
;
